package pATT.profilingAlgorithm.PersonalSearcher;


import java.io.File;
import java.io.IOException;

public class Settings {

	private static FileHashTable 	settingsHash;

	private static String 			directory = null;
	private static String 			active = "";
	private static boolean			connectionError = false;
	private static String 			msgConnError = null;

	private static double			comparison_threshold = 0.1;  // Agregado por Ferraggine - Stella
	private static double			similarity_threshold = 0.15;
	private static double			evaluation_threshold = 0.3;
	private static double			selection_threshold = 0.25;
	private static double			classification_threshold=0.5;
	//------------------Opciones de Configuracion------------------

	private static int 				amountOfPages;
	private static boolean 			proxyEnabled;
	private static int 				proxyType;
	private static String 			proxyAddress;
	private static String 			proxyPort;
	private static String 			pathNetscape;
	private static double			umbralInteres;
	private static boolean			passwordEnabled;
	private static String 			passwordLogin;
   	private static String 			passwordNumber;
	private static boolean 			inTotal;
	private static int				totalActiveEngines;

	private static int				countProfiles=0;

	//-------------------------------------------------------------

                public static int getCountProfiles()
                {
                   if (countProfiles==0)
                   {   try {
                        FileVector f=new FileVector();
                        f.load(getUsersFile());
                        countProfiles=f.size();
                        }
                       catch(IOException e){System.out.println(e.toString());}
                    }
                   return countProfiles;
              }

	static{
		settingsHash = new FileHashTable();
		try{
			String dir = "C:/Documents and Settings/Ignacio/Escritorio/Eclipse/PATT";
//        	settingsHash.load(System.getProperty("user.dir")+"/PSConfig.sys");
			settingsHash.load(dir+"/PSConfig.sys");
        }catch(IOException e){System.out.println("Error al abrir el archivo"+"/PSConfig.sys");}
	}

	public static String getDirectory(){
		if (directory==null){
			File f = new File(System.getProperty("user.dir"));
			directory = f.getPath()+"/";
		}
		return directory;
    }

	public static String getDataPath(){
		return getDirectory()+"data/";
	}

	public static String getUsersDirectory(){
		return getDirectory()+"users/";
	}

	public static String getUsersFile(){
		return 	"C:/Documents and Settings/Ignacio/Escritorio/Eclipse/PATT/users.dat";
		//return getDirectory()+"users.dat";
	}

	public static void setActiveUser(String s) /*throws IOException*/ {
		active = s;
	}

	public static String getActiveUser(){
		return active;
	}

	public static String getTemplate(String nameEngine){
		return (String)settingsHash.get(nameEngine.toUpperCase()+"TEMPLATE");
	}

		public static void setClassificationThreshold(double d){
				classification_threshold=d;
		}

		public static double getClassificationThreshold(){
				return classification_threshold;
		}

	public static int getStartNumber(String nameEngine){
		String s = (String)settingsHash.get(nameEngine.toUpperCase()+"CONFIG");
		return new Integer(s.substring(0,s.indexOf("|"))).intValue();
	}

	public static int getStepNumber(String nameEngine){
		String s = (String)settingsHash.get(nameEngine.toUpperCase()+"CONFIG");
		s = s.substring(s.indexOf("|")+1);
		return new Integer(s.substring(0,s.indexOf("|"))).intValue();
	}

	public static int getOverlapNumber(String nameEngine){
		String s = (String)settingsHash.get(nameEngine.toUpperCase()+"CONFIG");
		return new Integer(s.substring(s.lastIndexOf("|")+1)).intValue();
	}
	/*
	public static void setUserONE(String s) {
		userONE = s;
	}

	public static String getUserONE(){
		return userONE;
	}

	public static void setUserTWO(String s) {
		userTWO = s;
	}

	public static String getUserTWO(){
		return userTWO;
	}
	 */
	public static int getTotalActiveEngines(){
		return totalActiveEngines;
	}

	public static void setTotalActiveEngines(int i){
		totalActiveEngines=i;
	}

	//-----------Seteo de Opciones de Configuracion----------------

	public static int getAmountOfPages(){
		return amountOfPages;
	}

	public static int getAmountOfPagesInTotal(){
		return amountOfPages;
	}

	public static void setAmountOfPages(int i){
		amountOfPages=i;
	}

	public static boolean getProxyEnabled(){
		return proxyEnabled;
	}

	public static void setProxyEnabled(boolean b){
		proxyEnabled=b;
	}

	public static int getProxyType(){
		return proxyType;
	}

	public static void setProxyType(int i){
		proxyType=i;
	}

	public static String getProxyAddress(){
		return proxyAddress;
	}

	public static void setProxyAddress(String s){
		proxyAddress=s;
	}

	public static String getProxyPort(){
		return proxyPort;
	}

	public static void setProxyPort(String s){
		proxyPort=s;
	}

	public static String getPathNetscape(){
		return pathNetscape;
	}

	public static void setPathNetscape(String s){
		pathNetscape=s;
	}

	public static void setUmbralInteres(String s){
		umbralInteres=(new Double(s)).doubleValue() / 100;
	}

	public static double getUmbralInteres(){
		return umbralInteres;
	}

	public static boolean getPasswordEnabled(){
		return passwordEnabled;
	}

	public static void setPasswordEnabled(boolean b){
		passwordEnabled=b;
	}

	public static String getPasswordLogin(){
		return passwordLogin;
	}

	public static void setPasswordLogin(String s){
		passwordLogin=s;
	}

	public static String getPasswordNumber(){
		return passwordNumber;
	}

	public static void setPasswordNumber(String s){
		passwordNumber=s;
	}

	public static boolean getInTotal(){
		return inTotal;
	}

	public static void setInTotal(boolean b){
		inTotal=b;
	}

	public static void setSimilarityThreshold(double d){
		similarity_threshold=d;
	}
	
	public static double getSimilarityThreshold(){
		return similarity_threshold;
	}
	
	public static void setSelectionThreshold(double d){
		selection_threshold=d;
	}

	public static double getSelectionThreshold(){
		return selection_threshold;
	}
	
	public static void setEvaluationThreshold(double d){
		evaluation_threshold=d;
	}

	public static double getEvaluationThreshold(){
		return evaluation_threshold;
	}

	public static void setMsgConnectionError(String s){
		msgConnError=s;
	}

	public static String getMsgConnectionError(){
		if (msgConnError==null)
			return "Connection failed";
		else
			return msgConnError;
	}

	public static void setConnectionError(boolean change){
    	connectionError = change;
    }

	public static boolean getConnectionError(){
    	return connectionError;
    }

	public static void setConnection() {
//		setConnectionError(false);
//		CookieModule.setCookiePolicyHandler(null);
//		if (Settings.getProxyEnabled()){
//			if (Settings.getProxyType()==0)
//				HTTPConnection.setProxyServer(Settings.getProxyAddress(), new Integer(Settings.getProxyPort()).intValue());
//			else
//				HTTPConnection.setSocksServer(Settings.getProxyAddress(), new Integer(Settings.getProxyPort()).intValue());
//		}
//		if (Settings.getPasswordEnabled())
//			DefaultAuthHandler.setAuthorizationPrompter(new MyAuthPrompter(Settings.getPasswordLogin(),Settings.getPasswordNumber()));
//		else
//			DefaultAuthHandler.setAuthorizationPrompter(new MyAuthPrompter(null,null));
//
//		if (Settings.getPasswordEnabled()){
//			try{
//				URI url = new URI("http://some.host.dom/the/file.html");
//				HTTPConnection con = new HTTPConnection(url);
//				HTTPResponse   rsp = con.Head(url.getPathAndQuery());
//		//		int sts = rsp.getStatusCode();
//			}
//			catch(UnknownHostException e){
//				setConnectionError(true);
//				if (e.toString().indexOf("some.host.dom")<0)
//					setMsgConnectionError("Unknown Proxy Host");
//			}
//			catch(ParseException e){setConnectionError(true);}
//			catch(ModuleException e){setConnectionError(true);}
//			catch(ProtocolNotSuppException e){setConnectionError(true);}
//			catch(IOException e){setConnectionError(true);}
//		}
	}

	/**
	* Obtiene el umbral de similitud para realizar las recomendaciones entre usuarios
	*/
    public static double getComparisonThreshold(){
		return comparison_threshold;
	}

	/**
	* Setea el umbral de similitud para realizar las recomendaciones entre usuarios
	*/

    public static void setComparisonThreshold(double d){
		comparison_threshold = d;
	}
}

//class MyAuthPrompter implements AuthorizationPrompter {
//	private String  pa_name, pa_pass;
//    private boolean been_here = false;
//
//	MyAuthPrompter(String pa_name, String pa_pass) {
//		this.pa_name = pa_name;
//		this.pa_pass = pa_pass;
//    }
//
//    public NVPair getUsernamePassword(AuthorizationInfo challenge, boolean forProxy) {
//		if (forProxy && pa_name != null){
//			if (been_here) {
//	    		Settings.setMsgConnectionError("Proxy authorization failed");
//				return null;
//	    	}
//		
//			been_here = true;
//			return new NVPair(pa_name, pa_pass);
//		}
//
//		if (forProxy&&(!Settings.getPasswordEnabled())){
//				Settings.setMsgConnectionError("The proxy requires authorization");
//				return null;
//		}
//		if (challenge.getScheme().equalsIgnoreCase("Basic"))
//			AuthorizationInfo.addBasicAuthorization(challenge.getHost(), challenge.getPort(), challenge.getRealm(), pa_name, pa_pass);
//		else if (challenge.getScheme().equalsIgnoreCase("Digest"))
//			AuthorizationInfo.addDigestAuthorization(challenge.getHost(), challenge.getPort(), challenge.getRealm(), pa_name, pa_pass);
//		return null;
//    }
//}
//
